<?php
class PaylogAction extends CommonAction {

	// 消费记录
	public function payLog(){
		$payType    = intval($_REQUEST['payType']); //消费类型,0:充值,1:消费,-1:退款
		$cardId     = intval($_REQUEST['cardId']);
		$Paylog     = M('Paylog');
		$count      = $Paylog->where("cardId=$cardId AND payType=$payType")->count();
		if($count>0){
			import ( "@.ORG.Page" );
			$p      = new Page ( $count, C('PAGE_SIZE') );
			$logs   = $Paylog->where("cardId=$cardId AND payType=$payType")->order('payTime DESC')->limit($p->firstRow . ',' . $p->listRows)->findAll();
			//分页显示
			$page   = $p->show ();
			$this   ->assign ( "page", $page );
			$this   ->assign('logs', $logs);
		}
		if($payType==1){
			$this->display('ConsumeLog/consumeLog');
			return;
		}
		$this->display();
	}

	public function _before_add(){
		$Card = M('Card');
		$card = $Card->getById($_REQUEST['cardId']);
		$this->assign('card', $card);
	}
	public function insert(){
		$Paylog = D('Paylog');
		$Paylog->startTrans();
		if(false === $Paylog->create()){
			$this->error ( $Paylog->getError () );
		}
		//dump($_REQUEST); return;
		$result = $Paylog->add();
		if($result){  //更新会员卡信息
		$Card = M('Card');
			$r = $Card->setInc('money',"id={$_REQUEST['cardId']}",$_REQUEST["money"]);
			if($r){
				$Paylog->commit();
				$this->assign ( 'jumpUrl', Cookie::get ( '_currentUrl_/fff' ) );

				$this->success("充值成功");
			}
			return;
		}
		$Paylog->rollback();
		$this->error('充值失败');
	}

	//支付列表
	public function allPayLog(){
		if(isset($_REQUEST['downDate'])&&isset($_REQUEST['upDate'])){
			$downDate = strtotime($_REQUEST['downDate']);
			$upDate = strtotime("+1 day ".$_REQUEST['upDate']);
			if($upDate>$downDate){
				$where['_string']="payTime BETWEEN $downDate AND $upDate";
				$this->assign('upDate',$_REQUEST['upDate']);
				$this->assign('downDate',$_REQUEST['downDate']);
			}
		}
		//dump($_REQUEST);
		$cardId = isset($_REQUEST['cardId'])?intval($_REQUEST['cardId']):null;
		if($cardId){
			$where['cardId'] = $cardId;
		}
		$Paylog     = M('Paylog');
		$count      = $Paylog->where($where)->count();
		if($count>0){
			#====统计充值额====#
			$where['payType']=0;
			$allChargeLog = $Paylog->where($where)->select();
			$allChargeMoney=0;
			foreach($allChargeLog as $acl){
				$allChargeMoney += $acl['money'];
			}
			$this->assign('allChargeMoney', $allChargeMoney);
			#====统计充值额结束====#

			#====统计消费额====#
			$where['payType']=1;
			$allConsumeLog = $Paylog->where($where)->select();
			$allConsumeMoney=0;
			foreach($allConsumeLog as $acl){
				$allConsumeMoney += $acl['money'];
			}
			$this->assign('allConsumeMoney', $allConsumeMoney);
			#====统计消费额结束====#
			unset($where['payType']);
			import ( "@.ORG.Page" );
			$p      = new Page ( $count, C('PAGE_SIZE') );
			$logs   = $Paylog->where($where)->order('payTime DESC')->limit($p->firstRow . ',' . $p->listRows)->findAll();
			$User = D('User');
			$Member = D('Member');
			$length = count($logs);
			for($i=0; $i<$length; $i++){
				$logs[$i]['operator'] = $User->findById($logs[$i]['operatorId']);
				$logs[$i]['member'] = $Member->findById($logs[$i]['memberId']);
				$logs[$i]['saler']  = $User->findById($logs[$i]['salerId']);
			}
			//分页显示
			$page   = $p->show ();
			$this   ->assign ( "page", $page );
			$this   ->assign('logs', $logs);
		}

		$this->display();
	}

	public function index(){
		$this->redirect('allPayLog');
	}

	public function _before_fixPayLog(){
		$Product = M('Product');
		//$productList=$Product->where('status=1')->select();
		$productList=$Product->select();
		$this->assign('productList', $productList);
	}

	/**
	 * 修改支付记录
	 *
	 * 后期要加上修改记录,前期暂时搁下
	 */
	public function fixPayLog(){
		extract($_REQUEST,EXTR_SKIP);
		if($productId){
		//dump($_REQUEST);return;
			if($productId==$lastProductId){ //无修改
				echo "<script>alert('记录无修改');history.back();</script>";
				return;
			}else{
	//1,产品记录 产品名字，id
				header("Content-type:text/html;Chartset=utf-8");
				$Productlog = M('Productlog');
				$productLog = $Productlog->where("payId=$payId")->find();
				$Productlog->startTrans();
				$newProdctLogData['productId'] = $productId;
				$newProdctLogData['productName'] = $productName;
				$isSaveProductLog = $Productlog->where("id=".$productLog['id'])->save($newProdctLogData);

	//2,支付记录
			    $Paylog = M('Paylog');
			    $payLog = $Paylog->where('id='.$payId)->find();
			    $oldLogMoney = $payLog['money']; //原金额
				$newPayLogData['remark'] = $productName;
				$newPayLogData['money'] = $price;
				$isSavePayLog = $Paylog->where('id='.$payId)->save($newPayLogData);

	//3.卡余额
				$Product = M('Product');
				$product = $Product ->where('id='.$productId)->find();
				$newLogMoney = $product['price']; //新金额

				$diff = $oldLogMoney - $newLogMoney;

				$Card = M('Card');
				$isUpdateCard = $Card->setInc('money', "id=$cardId", $diff);
				if(!$isSaveProductLog || !$isSavePayLog || !$isUpdateCard){
					$Productlog->rollback();
					//echo "isSaveProductLog: ".$isSaveProductLog." isSavePayLog: ".$isSavePayLog." isUpdateCard:".$isUpdateCard;
					$this->assign('jumpUrl', '__URL__/allPayLog');
					$this->success('修改失败!');
				}else{
					$Productlog->commit();
					$this->assign('jumpUrl', '__URL__/allPayLog');
					$this->success('修改成功!');
					return;
				}
			}
			return;
		}

		$payId = $_REQUEST['payId'];
		$Paylog = D('Paylog');
		$Productlog = D('Productlog');
		$User = D('User');
		$Member = D('Member');

		$payLog = $Paylog->findById($payId);
		$productLog = $Productlog->findByPayId($payLog['id']);
		$member = $Member->findById($payLog['memberId']);
		$this->assign('payLog',$payLog);
		$this->assign('productLog',$productLog);
		$this->assign('member',$member);
		$this->display();
	}









}
?>